<?php

/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2021/9/30 上午10:59
 *@说明:商家管理控制器
 */
namespace app\admin\controller;


use app\common\model\Pick;
use app\common\util\AdminException;
use think\facade\Db;
use think\facade\View;
use think\exception\ValidateException;

class Store extends Base
{
    //待审核店铺列表
    public function store_check_list(){
        $status=input('status');
        $keyword=input('keyword');
        if ($status!=''){
            $where[]=['status','=',$status];
        }else{
            $where[] = ['status','in',"-1,2"];
        }
        if ($keyword){
            $where[]=['store_name','like',"%$keyword%"];
        }
        $goods_category=new \app\common\model\Store();
        $list=$goods_category::where($where)->order('store_id desc')->paginate([
            'query'=>['status'=>$status,'keyword'=>$keyword],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //待申请店铺详情
    public function store_check_detail(){
        $store_id=input('store_id');
        $store_model=new \app\common\model\Store();
        $info=$store_model->find($store_id);
        if (empty($info)){
            $this->error('商家不存在');
        }
        if (IS_POST){
            $data=input('post.');
            $status=input('status');
            if ($info['status']!=$status && $store_id>1){
                if ($info['status']!=-1){
                    throw new AdminException('审核通过或失败后不能再次审核');
                }
                if ($status==1){
                    //把商品重新更改为审核通过
//                    $goods_where[]=['store_id','=',$store_id];
//                    $goods_update['goods_state']=1;
//                    Db::name('goods')->where($goods_where)->update($goods_update);
                    $data['check_time']=time();
                }elseif ($status==2){
                    $data['refuse_time']=time();
                }
            }
            $res=$info->save($data);
            if ($res){
                adminLog("审核商家");
                throw new AdminException('操作成功',1,['url'=>url('Store/store_check_list')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        View::assign('info', $info);
        return view();
    }
    //店铺列表
    public function store_list(){
        $where=[];
        $keyword=input('keyword');
        $status=input('status');
        if ($status){
            $where[]=['status','=',$status];
        }
        if ($keyword){
            $where[]=['store_name','like',"%$keyword%"];
        }
        $goods_category=new \app\common\model\Store();
        $list=$goods_category::where($where)->order('store_id desc')->paginate([
            'query'=>['keyword'=>$keyword,'status'=>$status],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //店铺详情
    public function store_detail(){
        $store_id=input('store_id');
        $store_model=new \app\common\model\Store();
        $info=$store_model->find($store_id);
        if (empty($info)){
            $this->error('商家不存在');
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\Store::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                $result['result']=$error;
                throw new AdminException($values[0],10,$result);
            }
            $status=input('status');
            if ($info['status']!=$status && $store_id>1 && $status){
                if ($info['status']!=-1){
                    throw new AdminException('审核通过或失败后不能再次审核');
                }
                if ($status==1){
                    //把商品重新更改为审核通过
//                    $goods_where[]=['store_id','=',$store_id];
//                    $goods_update['goods_state']=1;
//                    Db::name('goods')->where($goods_where)->update($goods_update);
                    $data['check_time']=time();
                }elseif ($status==2){
                    $data['refuse_time']=time();
                }
            }
            $data['store_person_cert']=str_replace(SITE_URL,'',$data['store_person_cert']);
            $data['store_person_cert1']=str_replace(SITE_URL,'',$data['store_person_cert1']);
            $data['store_logo']=str_replace(SITE_URL,'',$data['store_logo']);
            $data['business_licence_cert']=str_replace(SITE_URL,'',$data['business_licence_cert']);
            $res=$info->save($data);
            if ($res){
                adminLog("编辑商家信息");
                throw new AdminException('操作成功',1,['url'=>url('Store/store_list')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        View::assign('info', $info);
        return view();
    }
    //自提点列表
    public function pick_list(){
        $where=[];
        $keyword=input('keyword');
        $status=input('status');
        if ($status){
            $where[]=['status','=',$status];
        }
        if ($keyword){
            $where[]=['store_name','like',"%$keyword%"];
        }
        $goods_category=new Pick();
        $list=$goods_category::where($where)->order('id desc')->paginate([
            'query'=>['keyword'=>$keyword,'status'=>$status],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //自提点详情
    public function pick_detail(){
        $id=input('id');
        $store_model=new Pick();
        $info=$store_model->find($id);
        if (empty($info)){
            $this->error('自提点不存在');
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\Pick::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                $result['result']=$error;
                throw new AdminException($values[0],10,$result);
            }
            $status=input('status');
            if ($info['status']!=$status && $status){
                if ($info['status']!=-1){
                    throw new AdminException('审核通过或失败后不能再次审核');
                }
                if ($status==1){
                    $data['check_time']=time();
                }elseif ($status==2){
                    $data['refuse_time']=time();
                }
            }
            $data['store_logo']=str_replace(SITE_URL,'',$data['store_logo']);
            $res=$info->save($data);
            if ($res){
                adminLog("编辑自提点信息");
                throw new AdminException('操作成功',1,['url'=>url('Store/pick_list')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        View::assign('info', $info);
        return view();
    }
    //商家分类列表
    public function store_class_list(){
        // 搜索条件
        $where = [];
        $open=input('open');
        $keyword=input('keyword');
        if ($open!=''){
            $where[]=['open','=',$open];
        }
        if ($keyword){
            $where[]=['name','like',"%$keyword%"];
        }
        $goods_category=new StoreClass();
        $list=$goods_category::where($where)->order('id desc')->paginate([
            'query'=>['open'=>$open,'keyword'=>$keyword],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //添加编辑商家分类
    public function add_edit_store_class(){
        $id=input('id');
        $goods_category=new StoreClass();
        if ($id){
            $info=$goods_category->find($id);
        }else{
            $info['open']=1;
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\StoreClass::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                return json(['code'=>10,'msg'=>$values[0],'result'=>$error]);
            }
            if ($id){
                $res=$info->save($data);
            }else{
                $res=$goods_category->save($data);
            }
            if ($res){
                adminLog($id?"编辑商家分类":"添加商家分类");
                return json(['code'=>1,'msg'=>'操作成功','url'=>url('Store/store_class_list')->build()]);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
        $cat_select = $goods_category->column('name,id');
        View::assign('cat_select',$cat_select);
        View::assign('info', $info);
        return view();
    }
}
